package com.joysuch.wwyt.mh.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.mh.dto.MhMonitorPointAndTypeDto;
import com.joysuch.wwyt.mh.enums.MonitordefaultTypeEnum;
import com.joysuch.wwyt.mh.mapper.CustomizeMonitorTypeMapper;
import com.joysuch.wwyt.mh.service.MonitorPointDataToDBService;
import com.joysuch.wwyt.mh.storage.MonitorPointData;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @author zero
 * @since 2021/7/22 17:05
 */
@Service
public class MonitorPointDataToDBServiceImpl implements MonitorPointDataToDBService {

    private static final double MAX_VALUE = 110;
    private static final double MIN_VALUE = 100;

    @Autowired
    private CustomizeMonitorTypeMapper customizeMonitorTypeMapper;
    /**
     * 随机获取20条监测点类型的数据，将其值赋给监测点数据值类
     * MhMonitorPointAndTypeBean的defaultType为"Y"时，MonitorPointData的value从给定的数值中随机获取
     * MhMonitorPointAndTypeBean的defaultType不为"Y"时，MonitorPointData的value从80到200随机获取
     * @return
     */

    /**
     * 复杂版模拟20条数据
     * @return  返回值格式如下
     *           List<Map<String,List<Map<String,Object>>>>
     */
    @Override
    public List<Map<String, Object>>  insertStochastic() {
        List<Map<String, Object>> monitoringPointSimulationDataList = new ArrayList<>();
        List<MhMonitorPointAndTypeDto> mhMonitorPointAndTypeDtos = customizeMonitorTypeMapper.getRandomData();
        for (MhMonitorPointAndTypeDto each : mhMonitorPointAndTypeDtos) {
            Map<String, Object> map = new HashMap<>();
            //"tag"是dto的pointCode
            map.put("tag", each.getPointCode());
            List<LinkedHashMap<String, Object>> values = new ArrayList<>();
            LinkedHashMap<String, Object> valOne = new LinkedHashMap<>();
            //time待定当前时间的秒数(long类型)
            valOne.put("time", System.currentTimeMillis() / 1000);
            //value按规则随机传入
            if ((MonitordefaultTypeEnum.STATE_TRANSITION.getValue()).equals(each.getDefaultType())) {
                String units = each.getUnits();
                if (StringUtil.isBlank(units)){
                    continue;
                }
                JSONObject unitsJsonObject = JSON.parseObject(units);
                Object[] unitsJsonObjects = unitsJsonObject.keySet().toArray();
                int Subscript = (int) (Math.random() * (unitsJsonObjects.length));
                String resultStr = (String) unitsJsonObjects[Subscript];
                double value = Double.parseDouble(resultStr);
                valOne.put("value", value);
            } else {
                Random rand = new Random();
                double result = MIN_VALUE + (rand.nextDouble() * (MAX_VALUE - MIN_VALUE));
                double value = (double) Math.round(result * 100) / 100;
                valOne.put("value", value);
            }
            valOne.put("orgCode", each.getOrgCode());
            valOne.put("tenentId", each.getTenentId());
            values.add(valOne);
            map.put("values", values);
            monitoringPointSimulationDataList.add(map);
        }
        return monitoringPointSimulationDataList;
    }


    /**
     * 简单版模拟20条数据
     * @return 模拟监测点数据值的List
     */
    @Override
    public List<MonitorPointData> addStochastic() {
        List<MhMonitorPointAndTypeDto> mhMonitorPointAndTypeDtos = customizeMonitorTypeMapper.getRandomData();
        List<MonitorPointData>  monitorPointDataList = new ArrayList<>();
        for (MhMonitorPointAndTypeDto each : mhMonitorPointAndTypeDtos) {
            MonitorPointData monitorPointData = new MonitorPointData();
            monitorPointData.setTime(System.currentTimeMillis()/1000);
            monitorPointData.setTenentId(Context.getCurrentTenantId());
            monitorPointData.setOrgCode(Context.getCurrentOrgCode());
            monitorPointData.setPointCode(each.getPointCode());
            if ((MonitordefaultTypeEnum.STATE_TRANSITION.getValue()).equals(each.getDefaultType())){
                String units = each.getUnits();
                JSONObject unitsJsonObject = JSON.parseObject(units);
                Object[] unitsJsonObjects = unitsJsonObject.keySet().toArray();
                int Subscript = (int)Math.random() * (unitsJsonObjects.length);
                String resultStr = (String) unitsJsonObjects[Subscript];
                double value = Double.parseDouble(resultStr);
                monitorPointData.setValue(value);
            }else {
                Random rand = new Random();
                double result = MIN_VALUE + (rand.nextDouble() * (MAX_VALUE - MIN_VALUE));
                double value = (double) Math.round(result * 100) / 100;
                monitorPointData.setValue(value);
            }
            monitorPointDataList.add(monitorPointData);
        }
        return monitorPointDataList;
    }
}
